<template>
  <div>
    <div class="top-right-btn">
      <el-button-group>
        <el-button size="mini" icon="el-icon-search" />
        <el-button size="mini" icon="el-icon-refresh" />
        <el-popover placement="bottom-end" width="150" trigger="click">
          <el-button slot="reference" size="mini" icon="el-icon-s-grid">
            <i class="fa fa-caret-down" aria-hidden="true" />
          </el-button>
          <el-checkbox v-model="checkAll" :indeterminate="isIndeterminate" @change="handleCheckAllChange">全选
          </el-checkbox>
          <el-checkbox-group v-model="checkedCities" @change="handleCheckedCitiesChange">
            <el-checkbox v-for="city in columnNames" :key="city" :label="city">{{ city }}</el-checkbox>
          </el-checkbox-group>
        </el-popover>
      </el-button-group>
    </div>
  </div>
</template>

<script>
export default {
  name: 'VTable',
  props: {
    columnNames: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      checkedCities: [],
      checkAll: false,
      isIndeterminate: true
    }
  },
  methods: {
    handleCheckAllChange(val) {
      this.checkedCities = val ? this.columnNames : []
      this.isIndeterminate = false
    },
    handleCheckedCitiesChange(value) {
      const checkedCount = value.length
      this.checkAll = checkedCount === this.columnNames.length
      this.isIndeterminate = checkedCount > 0 && checkedCount < this.columnNames.length
    }
  }
}
</script>

<style scoped>
.top-right-btn {
  margin-left: auto;
}
</style>
